<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="128px"
    >
      <el-form-item label="车型" prop="startDistance">
        <el-input
          v-model="queryParams.plateType"
          placeholder="请输入本地救援起步距离"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['driver:trailer:type:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="trailerList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
      <el-table-column label="车型" align="center" prop="vehicleType">
               
        <template slot-scope="scope">
                   
          <dict-tag
            :options="dict.type.vehicle_type"
            :value="scope.row.vehicleType"
          />
                 
        </template>
               
      </el-table-column>
      <el-table-column label="板长" align="center" prop="plateLength" />
      <el-table-column label="板宽" align="center" prop="plateWide" />
      <el-table-column label="车高" align="center" prop="trailerHeight" />
      <el-table-column label="承载" align="center" prop="bearing" />
      <el-table-column label="吨位" align="center" prop="tonnage">
               
        <template slot-scope="scope">
                   
          <dict-tag
            :options="dict.type.tonnage_type"
            :value="scope.row.tonnage"
          />
                 
        </template>
               
      </el-table-column>

      <el-table-column label="牌照颜色" align="center" prop="tonnage">
               
        <template slot-scope="scope">
                   
          <dict-tag
            :options="dict.type.license_plate_type"
            :value="scope.row.licensePlateColor"
          />
                 
        </template>
               
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆类型收费标准对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="60%"
      append-to-body
      custom-class="trailer-dialog"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px"
        class="trailer-form"
      >
        <el-divider content-position="center">
          <span class="divider-title">基本信息</span>
        </el-divider>
        <div class="form-container">
          <el-row :gutter="30">
            <el-col :span="8">
              <el-form-item label="车辆类型" prop="vehicleType">
                <el-select
                  v-model="form.vehicleType"
                  placeholder="请选择车辆类型"
                  clearable
                  class="full-width-input"
                >
                  <el-option
                    v-for="dict in dict.type.vehicle_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="牌照" prop="licensePlateColor">
                <el-select
                  v-model="form.licensePlateColor"
                  placeholder="请选择牌照类型"
                  clearable
                  class="full-width-input"
                >
                  <el-option
                    v-for="dict in dict.type.license_plate_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="吨位" prop="tonnage">
                <el-select
                  v-model="form.tonnage"
                  placeholder="请选择吨位"
                  clearable
                  class="full-width-input"
                >
                  <el-option
                    v-for="dict in dict.type.tonnage_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="30">
            <el-col :span="12">
              <el-form-item label="车辆图片" prop="img">
                <image-upload :limit="1" v-model="form.img" />
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <el-divider content-position="center">
          <span class="divider-title">车辆参数</span>
        </el-divider>
        <div class="form-container">
          <el-row :gutter="30">
            <el-col :span="6">
              <el-form-item label="板长" prop="plateLength">
                <el-input-number
                  :precision="2"
                  :step="0.5"
                  :max="20"
                  :min="0"
                  v-model="form.plateLength"
                  placeholder="请输入板长"
                  class="full-width-input"
                />
                <span class="unit-label">m</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="板宽" prop="plateWide">
                <el-input-number
                  :precision="2"
                  :step="0.5"
                  :max="10"
                  :min="0"
                  v-model="form.plateWide"
                  placeholder="请输入板宽"
                  class="full-width-input"
                />
                <span class="unit-label">m</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="车高" prop="trailerHeight">
                <el-input-number
                  :precision="2"
                  :step="0.5"
                  :max="5"
                  :min="0"
                  v-model="form.trailerHeight"
                  placeholder="请输入车高"
                  class="full-width-input"
                />
                <span class="unit-label">m</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="承载" prop="bearing">
                <el-input-number
                  :precision="2"
                  :step="1"
                  :max="100"
                  :min="0"
                  v-model="form.bearing"
                  placeholder="请输入承载"
                  class="full-width-input"
                />
                <span class="unit-label">t</span>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" icon="el-icon-check"
          >确 定</el-button
        >
        <el-button @click="cancel" icon="el-icon-close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPlateType,
  delTrailer,
  addTrailer,
  updateTrailer,
  addType,
  getType,
  updateType,
} from "@/api/driver/trailer/type";

export default {
  name: "Trailer",
  dicts: ["vehicle_type", "license_plate_type", "tonnage_type"],
  data() {
    return {
      form: {
        bearing: 0,
        licensePlateColor: "",
        plateLength: 0,
        plateWide: 0,
        tonnage: 0,
        trailerHeight: 0,
        trailerLength: 0,
        trailerWide: 0,
        vehicleType: "",
        img: "",
      },
      trailerTypeVO: {
        commissionDefault: {
          driverCommission: 0,
          driverCommissionType: 0,
          expressDriverCommission: 0,
          expressDriverCommissionType: 0,
          expressPlatformCommission: 0,
          expressPlatformCommissionType: 0,
          id: "",
          partnerCommission: 0,
          partnerCommissionType: 0,
          trailerTypeid: "",
        },
        serviceChargeDefault: {
          expressOutPrice: 0,
          expressStartDistance: 0,
          expressStartPrice: 0,
          id: "",
          outPrice: 0,
          startDistance: 0,
          startPrice: 0,
          trailerTypeid: "",
          type: 0,
        },
        trailerTypeVO: {
          bearing: 0,
          id: "",
          plateLength: 0,
          plateType: "",
          plateWide: 0,
          trailerHeight: 0,
          trailerLength: 0,
          trailerWide: 0,
        },
        userChargeDefault: {
          createBy: "",
          createTime: "",
          expressOutPrice: 0,
          expressStartDistance: 0,
          expressStartPrice: 0,
          id: "",
          outPrice: 0,

          startDistance: 0,
          startPrice: 0,
          trailerTypeid: "",
          type: 0,
        },
      },

      radio: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆类型收费标准表格数据
      trailerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        plateType: "",
        licensePlateColor: "",
        tonnage: "",
        vehicleType: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询车辆类型收费标准列表 */
    getList() {
      this.loading = true;
      let params = {
        licensePlateColor: this.queryParams.licensePlateColor,
        tonnage: this.queryParams.tonnage,
        vehicleType: this.queryParams.vehicleType,
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize
      };
      listPlateType(params).then((response) => {
        this.trailerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bearing: 0,
        createBy: "",
        createTime: "",
        delFlag: 0,
        licensePlateColor: "",
        params: {},
        plateLength: 0,
        plateWide: 0,
        remark: "",
        tonnage: 0,
        trailerHeight: 0,
        trailerLength: 0,
        trailerWide: 0,
        updateBy: "",
        updateTime: "",
        vehicleType: "",
        img: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆类型收费标准";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getType(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆类型收费标准";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.id) {
        updateType(this.form).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        addType(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除车辆类型收费标准编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTrailer(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "driver/trailer/export",
        {
          ...this.queryParams,
        },
        `trailer_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped>
.trailer-dialog {
  border-radius: 8px;
}

.trailer-form {
  padding: 0 20px;
}

.form-container {
  margin-bottom: 20px;
  background-color: #f9fafc;
  padding: 20px;
  border-radius: 6px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.divider-title {
  font-size: 16px;
  font-weight: bold;
  color: #409eff;
}

.el-divider {
  margin: 24px 0;
}

.unit-label {
  position: absolute;
  right: -20px;
  top: 0;
  line-height: 36px;
  color: #606266;
}

.full-width-input {
  width: 100%;
}

.dialog-footer {
  text-align: center;
  padding-top: 10px;
}

.el-input-number.full-width-input {
  width: 100%;
}
</style>
